<?php
/**
 * PROJECT      : php-framework
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * Created on	:  Aug 19, 2008
 *
 */

/**
 * Class for protecting image from Bandwidth Theft, and tool get image
 *
 */
class ImageProtector {
	
	/**
	 * Begin protect picture
	 *
	 * @param string $name optional
	 */
	function startProtect() {
		session_start ();
		$_SESSION ["__imgage_protector"] = 1;
	}
	
	/**
	 * Check if valid session
	 *
	 * @return boolean
	 */
	function isValidProtector() {
		session_start ();
		return $_SESSION ["__imgage_protector"];
	}
	
	/**
	 * Add the valid site that can be reference
	 *
	 * @param string $validprefix "prettytre.net" or "www.prettytre.net"
	 */
	function addValidReferenceSite($validprefix) {
		$allow = &ImageProtector::_getAllowSiteList ();
		$allow [] = $validprefix;
	}
	
	/**
	 * List allow site
	 *
	 * @return array
	 */
	function &_getAllowSiteList() {
		static $allowSite = array ();
		return $allowSite;
	}
	
	/**
	 * Get the image content for preview
	 *
	 * @param string $imageDir	 the folder point to image
	 * @param string $undefinedImgPath the path to image that don't display
	 * @param boolean $imgPathEncoded the path to image that don't display
	 * @param string $name the path to image that don't display
	 * @param string $email
	 */
	function getImage($imageDir, $undefinedImgPath = null, $imgPathEncoded = true, $name = "protected_image", $email = null) {
		if (! ImageProtector::isValidProtector ()) {
			if (isset ( $email )) {
				mail ( $email, "Bandwidth Theft Alert", "WARNING:\n\n$referrer\ntried to access\n$image\n", "From: CHImageGuard <$email>" );
			}
			if ($undefinedImgPath) {
				$imagepath = $imageDir . $undefinedImgPath;
				ImageProtector::_printImage ( $imagepath );
			} else {
				header ( "HTTP/1.0 404 Not Found" );
			}
			exit ();
		}
		
		$validprefixes = ImageProtector::_getAllowSiteList ();
		
		$image = $_GET [$name];
		if ($imgPathEncoded) {
			$image = base64_decode ( $image );
		}
		$referrer = getenv ( "HTTP_REFERER" );
		
		if (isset ( $image )) {
			
			if (empty ( $referrer ) || ImageProtector::isreferrerokay ( $referrer, $validprefixes )) {
				$imagepath = $imageDir . $image;
				ImageProtector::_printImage ( $imagepath );
			} else {
				if (isset ( $email )) {
					mail ( $email, "Bandwidth Theft Alert", "WARNING:\n\n$referrer\ntried to access\n$image\n", "From: CHImageGuard <$email>" );
				}
				if ($undefinedImgPath) {
					$imagepath = $imageDir . $undefinedImgPath;
					ImageProtector::_printImage ( $imagepath );
				} else {
					header ( "HTTP/1.0 404 Not Found" );
				}
			}
		}
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $referrer
	 * @param array $validprefixes
	 * @return boolean
	 */
	function isreferrerokay($referrer, $validprefixes) {
		if (! count ( $validprefixes )) {
			return true;
		}
		$validreferrer = 0;
		$authreferrer = current ( $validprefixes );
		while ( $authreferrer ) {
			if (eregi ( "^https?://$authreferrer/", $referrer )) {
				$validreferrer = 1;
				break;
			}
			$authreferrer = next ( $validprefixes );
		}
		return $validreferrer;
	}
	
	/**
	 * Function for print the image into stream
	 *
	 * @param string $imagepath
	 */
	function _printImage($imagepath) {
		$imageinfo = getimagesize ( $imagepath );
		if ($imageinfo [2] == 1) {
			$imagetype = "gif";
		} elseif ($imageinfo [2] == 2) {
			$imagetype = "jpeg";
		} elseif ($imageinfo [2] == 3) {
			$imagetype = "png";
		} else {
			header ( "HTTP/1.0 404 Not Found" );
			exit ();
		}
		
		header ( "Content-type: image/$imagetype" );
		@readfile ( $imagepath );
	}
	
	/**
	 * Get the link to the real image
	 *
	 */
	function getImageLink($imgPath, $name = "protected_image", $imgPathEncoded = true) {
		if (strpos ( $imgPath, 'http' ) === 0) {
			return $imgPath;
		}
		if ($imgPathEncoded) {
			$imgPath = base64_encode ( $imgPath );
		}
		return SYS_ROOT_URL . "core/runAlone/image_protector.php?$name=$imgPath";
	}
}
?>